package com.canbot.u05.activity.guide;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.presidemode.eventbus.AudioEvent;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.StringUtils;
import com.canbot.u05.utils.ToastTools;

/**
 * Created by zmp on 2019/4/10 17:08
 *
 * @author zmp
 * <p>
 * 导览语音选择界面
 */
public class GuideChooseTtsActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private ToastTools toastTools;

        private EditText ttsEditText;

        public static final int MAX_LEN = 2000;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_guide_choose_tts);
                EditText editText = findViewById(R.id.ttsEditText);
                editText.setHint(StringUtils.getLinkRobotStrTwo(R.string.guide_tts_hint));
                toastTools = ToastTools.newInstance(this);
                initView();

        }

        private void initView() {
                findViewById(R.id.guide_tts_save).setOnClickListener(this);
                findViewById(R.id.guide_tts_back).setOnClickListener(this);
                findViewById(R.id.guide_tts_play_iv).setOnClickListener(this);
                ttsEditText = (EditText) findViewById(R.id.ttsEditText);
                ttsEditText.addTextChangedListener(watcher);
                Intent intent = getIntent();
                int type = intent.getIntExtra("type", AudioEvent.AUDIO_TYPE_TTS);
                if (type == AudioEvent.AUDIO_TYPE_TTS) {
                        String content = intent.getStringExtra("content");
                        ttsEditText.setText(content);
                }
        }


        private TextWatcher watcher = new TextWatcher() {
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > MAX_LEN) {
                                ttsEditText.setText(s.subSequence(0, MAX_LEN));
                                ttsEditText.setSelection(MAX_LEN);
                                toastTools.showText(getString(R.string.input_maximum_words));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.guide_tts_save:
                                String trim = ttsEditText.getText().toString().trim();
                                if (TextUtils.isEmpty(trim)) {
                                        toastTools.showText(getString(R.string.enter_text_content));
                                        return;
                                }
                                setVoiceResult(trim);
                                break;
                        case R.id.guide_tts_back:
                                setResult(RESULT_CANCELED);
                                finish();
                                break;
                        case R.id.guide_tts_play_iv:
                                String trim2 = ttsEditText.getText().toString().trim();
                                if (TextUtils.isEmpty(trim2)) {
                                        toastTools.showText(getString(R.string.text_cannot_empty));
                                        return;
                                }
                                MsgSendUtils.sendStringMsg(MsgType.PLAY_TTS, trim2);
                                break;
                        default:
                }
        }

        private void setVoiceResult(String content) {
                setResult(RESULT_OK, new Intent()
                        .putExtra("type", AudioEvent.AUDIO_TYPE_TTS)
                        .putExtra("content", content));
                finish();
        }

}
